package com.lemon.smartsafetynew.ui.activity.ledger;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingCheckItemVo;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingItemVo;
import com.lemon.smartsafetynew.ui.fragment.ledger.LedgerBuildingCheckItemListFragment;
import com.lemon.smartsafetynew.util.common.ColorUtil;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 企业台账
 * Created by Andrew on 2017/9/4.
 */

public class LedgerBuildingCheckItemListActivity extends SwipeBackActivity
        implements RecyclerTClickListener<LedgerBuildingCheckItemVo> {

    @Bind(R.id.location_imageView)
    ImageView locationImageView;
    @Bind(R.id.monitor_item_tv)
    TextView monitorItemTv;
    @Bind(R.id.monitor_item_count_tv)
    TextView monitorItemCountTv;
    @Bind(R.id.pie_chart)
    PieChart pieChart;
    @Bind(R.id.normal_tv)
    TextView normalTv;
    @Bind(R.id.abnormal_tv)
    TextView abnormalTv;
    @Bind(R.id.frameLayout)
    FrameLayout frameLayout;

    LedgerBuildingCheckItemListFragment ledgerBuildingCheckItemListFragment;

    private static LedgerBuildingItemVo mLedgerBuildingItemVo;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            if(bundle.containsKey(KeyConstants.KEY_LEDGER_PATROL_BUILDING_VO)) {
                mLedgerBuildingItemVo = (LedgerBuildingItemVo) bundle.getSerializable(KeyConstants.KEY_LEDGER_PATROL_BUILDING_VO);
            }
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(getString(R.string.work_options_main_3));
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerBuildingCheckItemListActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_ledger_building_check_item_list;
    }

    @Override
    protected void initViews() {
        super.initViews();

        // disable by Andrew.yin 2017/10/21 begin 常熟项目不需要
        //initPieChart();
        //initCommonView();
        // disable by Andrew.yin 2017/10/21 end

        initFragment();
    }


    private void initCommonView() {

        monitorItemTv.setText(CommonUtil.avoidNullString(mLedgerBuildingItemVo.getBuilding_name()));
        monitorItemCountTv.setText("巡查点总数：" + CommonUtil.avoidNullString(mLedgerBuildingItemVo.getItem_num()));
        normalTv.setText("正常：" + CommonUtil.avoidNullString(mLedgerBuildingItemVo.getGreen_item_num()));
        abnormalTv.setText("异常：" + CommonUtil.avoidNullString(mLedgerBuildingItemVo.getRed_item_num()));

        List<Integer> list = new ArrayList<>();
        if(mLedgerBuildingItemVo.getGreen_item_num() != null &&
                mLedgerBuildingItemVo.getRed_item_num() != null) {

            list.add(Integer.valueOf(mLedgerBuildingItemVo.getGreen_item_num()));
            list.add(Integer.valueOf(mLedgerBuildingItemVo.getRed_item_num()));
            setPieChartData(list);
        } else {
            setPieChartData(list);
        }

    }

    private void initFragment() {
        ledgerBuildingCheckItemListFragment = LedgerBuildingCheckItemListFragment.newInstance(mLedgerBuildingItemVo);
        ledgerBuildingCheckItemListFragment.setListener(this);
        getSupportFragmentManager().beginTransaction().replace(R.id.frameLayout, ledgerBuildingCheckItemListFragment).commit();
    }

    // ======== PieChart ========
    private void initPieChart() {

        pieChart.setNoDataText("无数据");
        pieChart.setUsePercentValues(true);
        pieChart.getDescription().setEnabled(false);
        pieChart.setCenterText("检查点概况");
        pieChart.setDrawHoleEnabled(true);
        pieChart.setHoleColor(Color.WHITE);
        pieChart.setTransparentCircleColor(Color.WHITE);
        pieChart.setTransparentCircleAlpha(110);
        pieChart.setHoleRadius(58f);
        pieChart.setTransparentCircleRadius(61f);
        pieChart.setDrawCenterText(true);
        pieChart.setRotationAngle(0);
        // enable rotation of the chart by touch
        pieChart.setRotationEnabled(false);
        pieChart.setHighlightPerTapEnabled(false);

        Legend l = pieChart.getLegend();
        l.setEnabled(false);

        // entry label styling
        pieChart.setEntryLabelColor(Color.WHITE);
        pieChart.setEntryLabelTextSize(12f);

    }

    private void setPieChartData(List<Integer> list) {

        ArrayList<PieEntry> entries = new ArrayList<>();

        // NOTE: The order of the entries when being added to the entries array determines their position around the center of
        // the chart.
        for (int i = 0; i < list.size(); i++) {
            entries.add(new PieEntry(list.get(i)));
        }

        PieDataSet dataSet = new PieDataSet(entries, "");
        dataSet.setDrawValues(false);

        // add a lot of colors
        ArrayList<Integer> colors = new ArrayList<>();

        colors.add(Color.parseColor(ColorUtil.ledgerSafeBuildingPieChart[0])); //正常
        colors.add(Color.parseColor(ColorUtil.ledgerSafeBuildingPieChart[1])); //异常

        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter());
        data.setValueTextSize(11f);
        data.setValueTextColor(Color.WHITE);
        pieChart.setData(data);

        // undo all highlights
        pieChart.highlightValues(null);

        pieChart.invalidate();
    }

    @Override
    public void onItemClick(LedgerBuildingCheckItemVo data, int position) {

        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_LEDGER_PATROL_CHECK_ITEM_VO, data);
        bundle.putSerializable(KeyConstants.KEY_LEDGER_PATROL_BUILDING_VO, mLedgerBuildingItemVo);
        UIHelper.showActivity(LedgerBuildingCheckItemListActivity.this, bundle, LedgerBuildingCheckItemDetailActivity.class);
    }
}
